package WaitNotify;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2022-12-25
 * Time: 12:31
 */
public class TestDemo7 {

    private static final Object lock = new Object();
    private static boolean isCigarette;
    private static boolean isHasEat;

    public static void main(String[] args) throws InterruptedException {
        //小南线程等待香烟
        new Thread(()->{
            synchronized (lock) {
                System.out.println("是否有香烟" + isCigarette);
                while (!isCigarette){
                    System.out.println("没有烟,等待一会");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("是否有香烟" + isCigarette);
                if(isCigarette) {
                    System.out.println("小南开始干活");
                }
            }
        },"小南").start();

        //小女线程等待外卖
        new Thread(()->{
            synchronized (lock) {
                System.out.println("是否有外卖" + isHasEat);
                while(!isHasEat){
                    System.out.println("没有外卖,等待一会");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("是否有外卖" + isHasEat);
                if(isHasEat) {
                    System.out.println("小女吃完外卖,开始干活");
                }
            }
        },"小女").start();
        Thread.sleep(1000);
        //另外一个线程送外卖的
        new Thread(()->{
            synchronized (lock) {
                isHasEat = true;
                lock.notifyAll();
            }
        },"外卖小哥").start();

        Thread.sleep(1000);
        synchronized (lock) {
            isCigarette = true;
            lock.notifyAll();
        }
    }
}
